package cn.findviewbydream.evil.app.service.impl;

import cn.findviewbydream.evil.app.dao.ChefDAO;
import cn.findviewbydream.evil.app.dto.ChefOrderBean;
import cn.findviewbydream.evil.app.enums.OrderItemStateType;
import cn.findviewbydream.evil.app.service.ChefService;
import cn.findviewbydream.evil.app.websocket.WebSocketHandlerImpl;
import cn.findviewbydream.evil.main.entity.OrderEntity;
import cn.findviewbydream.evil.main.entity.OrderItemEntity;
import cn.findviewbydream.evil.mealorderofwaiter.enums.SocketEventType;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/********************************************
 * 工程名：MealOrder
 * 包名：cn.findviewbydream.evil.app.service.impl
 * 时间：17-1-22
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：
 ********************************************/
@Service
public class ChefServiceImpl implements ChefService
{
    private final ChefDAO chefDAO;

    @Autowired
    public ChefServiceImpl(ChefDAO chefDAO) {this.chefDAO = chefDAO;}

    @Nullable
    @Override
    public synchronized List<ChefOrderBean> getOrderBeanList(Integer typeOfWorkId)
    {
        //long start = System.currentTimeMillis();
        List<ChefOrderBean> list = new ArrayList<>();
        ChefOrderBean chefOrderBean;

        if (typeOfWorkId == null)
        {
            List<OrderEntity> orderList = chefDAO.selectOrderList();
            for (OrderEntity orderEntity : orderList)
            {
                if (orderEntity.getOrderItemList() != null)
                    for (OrderItemEntity orderItemEntity : orderEntity.getOrderItemList())
                    {
                        chefOrderBean = new ChefOrderBean();
                        chefOrderBean.setOrderId(orderEntity.getOrderId());
                        chefOrderBean.setSeatId(orderEntity.getSeatId());
                        chefOrderBean.setWaiter(orderEntity.getWaiter());
                        chefOrderBean.setCreateDateTime(orderEntity.getCreateDateTime());

                        chefOrderBean.setOrderItemId(orderItemEntity.getOrderItemId());
                        chefOrderBean.setMealId(orderItemEntity.getMealId());
                        chefOrderBean.setMealNum(orderItemEntity.getMealNum());
                        chefOrderBean.setMealTotal(orderItemEntity.getMealTotal());
                        chefOrderBean.setState(orderItemEntity.getState());
                        list.add(chefOrderBean);
                    }
            }
            return list;
        }

        List<OrderEntity> orderList = chefDAO.selectOrderList();
        List<OrderItemEntity> orderItemList;

        for (OrderEntity orderEntity : orderList)
        {
            orderItemList = orderEntity.getOrderItemList();

            if (orderItemList != null && orderItemList.size() > 0)
            {
                for (OrderItemEntity orderItemEntity : orderItemList)
                {
                    if (orderItemEntity.getMealId() != null)
                    {
                        if (orderItemEntity.getMealId().getCookType() != null)
                        {
                            if (orderItemEntity.getMealId().getCookType().getTypeOfWorkId() == typeOfWorkId)
                            {
                                chefOrderBean = new ChefOrderBean();
                                chefOrderBean.setOrderId(orderEntity.getOrderId());
                                chefOrderBean.setSeatId(orderEntity.getSeatId());
                                chefOrderBean.setWaiter(orderEntity.getWaiter());
                                chefOrderBean.setCreateDateTime(orderEntity.getCreateDateTime());

                                chefOrderBean.setOrderItemId(orderItemEntity.getOrderItemId());
                                chefOrderBean.setMealId(orderItemEntity.getMealId());
                                chefOrderBean.setMealNum(orderItemEntity.getMealNum());
                                chefOrderBean.setMealTotal(orderItemEntity.getMealTotal());
                                chefOrderBean.setState(orderItemEntity.getState());
                                list.add(chefOrderBean);
                            }
                        }
                    }
                }
            }
        }

        /*GC*/
        {
            orderList = null;
            orderItemList = null;
        }

        //long end = System.currentTimeMillis();
        //System.out.println(end - start + "ms");
        return list;
    }

    @Nullable
    @Override
    public Boolean postOrderItemState(int orderItemId, int state)
    {
        int count = chefDAO.updateOrderItemState(orderItemId, state);

        /*成功插入后->发送Socket 状态103 刷新已完成订单列表*/
        WebSocketHandlerImpl.Companion.sendMessageToUsers(SocketEventType.COMPLETE.getIndex());

        /*发送Socket 统计完成的菜品的数量 格式-> COMNUM:数量 以便于更新toolbar*/
        if (state == OrderItemStateType.COMPLETE_MAKE)
            WebSocketHandlerImpl.Companion.sendMessageToUsers
                    ("COMNUM:" + chefDAO.selectOrderItemStateNum(OrderItemStateType.COMPLETE_MAKE));

        return count == 1;
    }
}
